﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

public partial class Controls_BackEnd_FuntionEdit : UserControl
{
    private const string ControlOrder = "functionorder.ascx";



    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        GetData();
    }


    /// <summary>
    /// 
    /// </summary>
    private void GetData()
    {
        BindFather();
        Functions = Config.Id > 0 ? new Functions { FunctionID = Config.Id }.FunctionsGetByFunctionID() :
        new Functions();
        CheckPermission();

    }


    /// <summary>
    /// 
    /// </summary>
    private void CheckPermission()
    {

        var permission = (UserFunctions)Session[SessionManager.SessionPermission];
        btnSave.Visible = Config.Id > 0 ? permission.IsUpdate : permission.IsInsert;
        btnReset.Visible = permission.IsInsert;
    }


    /// <summary>
    /// 
    /// </summary>
    protected Functions Functions
    {
        get
        {
            var fun = (Functions)Session[SessionManager.SessionData] ?? new Functions();
            fun.FatherID = int.Parse(ddlFather.SelectedValue);
            fun.Name = txtName.Text;
            fun.Url = txtUrl.Text;
            fun.IsDisplay = cbDisplay.Checked;
            fun.IsActive = cbActive.Checked;
            return fun;
        }
        set
        {
            Session[SessionManager.SessionData] = value;
            txtUrl.Text = value.Url;
            txtName.Text = value.Name;
            cbDisplay.Checked = value.IsDisplay;
            cbActive.Checked = value.IsActive;
            ddlFather.SelectedValue = value.FatherID.ToString();
        }
    }




    /// <summary>
    /// 
    /// </summary>
    private void BindFather()
    {
        ddlFather.Items.Clear();
        ddlFather.Items.Add(new ListItem { Value = @"0", Text = @"--- Select father control ---" });
        var list = new Functions().Functions_GetByCondition(" IsDisplay=1");
        foreach (var function in list)
            ddlFather.Items.Add(new ListItem { Value = function.FunctionID.ToString(), Text = function.Name });

    }




    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AllCommand(object sender, EventArgs e)
    {
        switch (((Control)sender).ID)
        {
            case "btnReset":
                Functions = new Functions();
                Config.Id = 0;
                BindFather();
                break;
            case "btnBack":
                Config.Back();
                break;
            case "lbtnOrder":
                Config.Redirect(new Functions().GetFunctionID(ControlOrder), 0);
                break;
            case "btnSave":
                var Error = Functions.Save();
                if (Error > 0)
                {
                    Functions.FunctionID = Config.Id = Error;
                    Config.Message(Page, Resources.Resource.SaveSuccess, Resources.Resource.Messenger);
                    return;
                }
                switch (Error)
                {
                    case -1:
                        Config.Message(Page, string.Format(Resources.Resource.ObjectExisted, Resources.Resource.Function), Resources.Resource.Warning);
                        break;
                    default:
                        Config.Message(Page, Resources.Resource.ErrorMsg, Resources.Resource.Warning);
                        break;
                }
                break;

        }

    }


}
